<template>
  <div>
    <ul>
      <li>
        <a href="javascript:;" @click="btn('/contacts/allcontacts')"
          >AllContacts</a
        >
      </li>
      <li><a href="javascript:;" @click="btn('/contacts/alice')">Alice</a></li>
      <li><a href="javascript:;" @click="btn('/contacts/bob')">Bob</a></li>
    </ul>
    <button @click="change">随机显示</button>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 数组接存放地址
      pathArr: ["allcontacts", "alice", "bob"],
    };
  },
  methods: {
    // 点击切换路径
    btn(path) {
      this.$router
        .push({
          path,
        })
        // 空catch置空报错
        .catch(() => {});
    },
    change() {
      // 根据存放地址的数组的长度生成随机数
      let num = Math.floor(Math.random() * this.pathArr.length);
      console.log(this.pathArr[num]);
      this.$router
        .push({
          // 地址接收数组指定索引的地址
          path: this.pathArr[num],
        })
        .catch(() => {});
    },
  },
};
</script>

<style>
</style>